package com.dlc.shop.user.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.user.common.dao.PopupRelateMapper;
import com.dlc.shop.user.common.model.PopupRelate;
import com.dlc.shop.user.common.service.PopupRelateService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 弹窗广告关联
 * @author TRACK
 */
@Service
@AllArgsConstructor
public class PopupRelateServiceImpl extends ServiceImpl<PopupRelateMapper, PopupRelate> implements PopupRelateService {

    private final PopupRelateMapper popupRelateMapper;

    @Override
    public List<PopupRelate> getByPopupId(Long popupId) {
        return popupRelateMapper.getByPopupId(popupId);
    }

    @Override
    public void saveBatch(List<PopupRelate> popupRelates) {
        popupRelateMapper.saveBatch(popupRelates);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatch(Long popupId, List<PopupRelate> popupRelates) {
        // 先删除后新增
        popupRelateMapper.deleteById(popupId);
        if (CollUtil.isEmpty(popupRelates)) {
            return;
        }
        popupRelateMapper.saveBatch(popupRelates);
    }

    @Override
    public void deleteById(Long popupId) {
        popupRelateMapper.deleteById(popupId);
    }

    @Override
    public void deleteByRelateIds(List<Long> relateIds) {
        popupRelateMapper.deleteByRelateIds(relateIds);
    }
}
